import wepy from 'wepy'

export default class extends wepy.mixin {
  data = {
    cart: []
  }

  onLoad() {
    this.cart = this.$parent.globalData.cart
  }

  methods = {
    cartChange(e) {
      // console.log(e)
      const id = e.target.dataset.id
      const count = e.detail
      this.$parent.updateGoodCount(id, count)
    },

    statusChange(e) {
      const status = e.detail
      const id = e.target.dataset.id
      this.$parent.updateGoodStatus(id, status)
    },

    delItem(id) {
      // console.log(id);
      this.$parent.removeGoodById(id)
    },

    fullCheckChange(e) {
      this.$parent.updateAllGoodStatus(e.detail)
    },

    submitOrder() {
      if (this.TotalPrice <= 0) {
        return wepy.baseToast('订单金额不能为空！')
      }

      wepy.navigateTo({
        url: '/pages/order'
      })
    }
  }

  computed = {
    isEmpty() {
      if (this.cart.length <= 0) {
        return true
      }
      return false
    },

    // 计算购物车总价
    TotalPrice() {
      let total = 0
      this.cart.forEach(x => {
        if (x.isCheck) {
          total += x.price * x.count
        }
      })
      return total * 100
    },

    // 是否被全选
    isFullCheck() {
      let allCount = this.cart.length
      let c = 0
      this.cart.forEach(x => {
        if (x.isCheck) {
          c++
        }
      })
      return allCount === c
    }
  }
}
